package com.bamboowind.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author cpw
 * @Date 2022/11/2 17:17
 * @Describe: 延时队列配置类
 */
@Configuration
public class DelayedQueueConfig {

    /**
     * 延时队列名称
     */
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";

    /**
     * 延时交换机名称
     */
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";

    /**
     * 延时路由key
     */
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    /**
     * @return 创建延时队列
     */
    @Bean
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }

    /**
     * @return 创建自定义延时交换机
     */
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>(1);
        // 自定义交换机类型
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    /**
     * @return 将延时队列与延时交换机进行绑定
     */
    @Bean
    public Binding bindingDelayedQueue() {
        return BindingBuilder.bind(delayedQueue()).to(delayedExchange()).with(DELAYED_ROUTING_KEY).noargs();
    }
}
